package itsm.isperp.module.controller.api.test;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.utils.JsonBuilder;
import itsm.isperp.module.controller.api.mobile.MobileSystemController;
import itsm.isperp.module.entity.app.AppDict;
import itsm.isperp.module.entity.app.AppUser;
import itsm.isperp.module.service.app.AppDictService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2015-3-16
 * @since 1.0
 * 
 */
@Controller
@RequestMapping("api/test/performance")
public class PerformanceController {

	public static final Logger logger = LoggerFactory
			.getLogger(MobileSystemController.class);

	@RequestMapping("json")
	@ResponseBody
	public String json(HttpServletRequest req, HttpServletResponse resp) {

		String str = null;
		String c = req.getParameter("c");
		String d = req.getParameter("d");
		String j = req.getParameter("j");
		String s = req.getParameter("s");
		str = generalTestData(req, c, d, j, s);
		return str;
	}

	@RequestMapping("page")
	public ModelAndView page(HttpServletRequest req, HttpServletResponse resp) {
		ModelAndView mv = new ModelAndView("api/test/performance/page");
		String str = null;
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String c = req.getParameter("c");
		String d = req.getParameter("d");
		String j = req.getParameter("j");
		String s = req.getParameter("s");
		str = generalTestData(req, c, d, j, s);

		mv.addObject("str", str);

		return mv;
	}

	private String generalTestData(HttpServletRequest req, String cache,
			String data, String json, String s) {

		if ("clear".equals(s)) {

			req.getSession().invalidate();

		} else if ("addnew".equals(s)) {
			req.getSession(true).setAttribute("a", "session");

		} else if ("add".equals(s)) {
			req.getSession(false).setAttribute("a", "session");
		}

		StringBuilder sb = new StringBuilder();
		if (StringUtils.isNoneEmpty(cache)) {

			sb.append("from cache string:"
					+ ContextHolder.getConfigByKey("apkFilePath"));
			sb.append("-------");
		}
		if (StringUtils.isNoneEmpty(data)) {
			AppDictService appDictService = ContextHolder
					.getSpringBean("appDictService");

			List<AppDict> dicts = appDictService.findByType("SOLUTION_WAY");

			sb.append("----------query date:" + dicts.size() + "-------");
		}

		if (StringUtils.isNoneEmpty(json)) {
			List<AppUser> list = new ArrayList<AppUser>();
			AppUser u = new AppUser();
			// u.setId(HibernateUUIDGenerator.generate());
			u.setName("test");
			u.setFullname("tester");
			u.setBirthday(new Date());

			list.add(u);

			String j = JsonBuilder.buildNormalBinder().toJson(list);
			sb.append("use jsonbuilder:" + j);
			sb.append("-------");
		}

		return sb.toString();

	}
}
